package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.Commodity;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.PurchaseCoupon;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.*;
import com.example.xpvideo.entity.res.CommodityListRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.PurchaseCouponMapper;
import com.example.xpvideo.service.PurchaseCouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 优惠券表
(PurchaseCoupon)Service层
 *
 * @author 杨丽辉
 * @since 2021-07-22 14:12:46
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class PurchaseCouponServiceImpl implements PurchaseCouponService {
    @Autowired
    private PurchaseCouponMapper purchaseCouponMapper;

    @Override
    public ResponseResult<PageResult<List<PurchaseCoupon>>> list(PublicReq publicReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();

        PageHelper.startPage(page, size);

        List<PurchaseCoupon> list = purchaseCouponMapper.selectList(Wrappers.<PurchaseCoupon>query());

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PurchaseCoupon>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> pfullCounponInsert(FullCouponReq fullCouponReq) throws ErrorException{

        BigDecimal fullReduction = fullCouponReq.getFullReduction();
        BigDecimal couponQuota = fullCouponReq.getCouponQuota();
        Integer count = fullCouponReq.getCouponCount();
        LocalDateTime startTime = fullCouponReq.getStartTime();
        LocalDateTime endTime = fullCouponReq.getEndTime();
        Integer type = fullCouponReq.getCouponType();
        String use = fullCouponReq.getInstructionsForUse();

        PurchaseCoupon purchaseCoupon = new PurchaseCoupon();
        purchaseCoupon.setCouponCount(count);
        purchaseCoupon.setTotalInventory(count);
        purchaseCoupon.setCouponQuota(couponQuota);
        purchaseCoupon.setCouponStartTime(startTime);
        purchaseCoupon.setCouponEndTime(endTime);
        purchaseCoupon.setFullReduction(fullReduction);
        purchaseCoupon.setInstructionsForUse(use);
        purchaseCoupon.setCouponType(type);
        purchaseCoupon.setCouponStage(2);
        purchaseCoupon.setEffectType(1);
        purchaseCoupon.setCreatTime(LocalDateTime.now());

        int res = purchaseCouponMapper.insert(purchaseCoupon);

        if(res > 0){
            return ResponseResult.responseResult(0,"上架满减优惠券成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20002);
    }

    @Override
    public ResponseResult<Object> pInstantCounponInsert(PInstantCouponReq pInstantCouponReq) throws ErrorException{

        BigDecimal couponQuota = pInstantCouponReq.getCouponQuota();
        Integer count = pInstantCouponReq.getCommodityTotalNum();
        LocalDateTime startTime = pInstantCouponReq.getStartTime();
        LocalDateTime endTime = pInstantCouponReq.getEndTime();
        Integer type = pInstantCouponReq.getCouponType();
        String use = pInstantCouponReq.getInstructionsForUse();

        PurchaseCoupon purchaseCoupon = new PurchaseCoupon();
        purchaseCoupon.setCouponCount(count);
        purchaseCoupon.setTotalInventory(count);
        purchaseCoupon.setCouponQuota(couponQuota);
        purchaseCoupon.setCouponStartTime(startTime);
        purchaseCoupon.setCouponEndTime(endTime);
        purchaseCoupon.setCouponType(type);
        purchaseCoupon.setInstructionsForUse(use);
        purchaseCoupon.setCouponStage(2);
        purchaseCoupon.setEffectType(2);
        purchaseCoupon.setCreatTime(LocalDateTime.now());

        int res = purchaseCouponMapper.insert(purchaseCoupon);

        if(res > 0){
            return ResponseResult.responseResult(0,"上架立减优惠券成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20002);
    }

}

